package com.shinbada.modules.competitor.web;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.excel.ExcelKit;
import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.CompetitorNoumenon;
import com.shinbada.modules.competitor.entity.DTO.CompetitorNoumenonDTO;
import com.shinbada.modules.competitor.entity.DTO.ParamModelSelectionDTO;
import com.shinbada.modules.competitor.entity.VO.CompetitorDataItemVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorNoumenonResultVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorProductDataItemVO;
import com.shinbada.modules.competitor.entity.VO.ConditionTreeVO;
import com.shinbada.modules.competitor.service.CompetitorNoumenonService;
import com.shinbada.modules.competitor.utils.TreeUtilCommon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 本体Controller
 *
 * @author initcap
 * @version 2021-08-11
 */
@RestController
@RequestMapping(value = "/competitor/competitorNoumenon")
@Api(tags = "本体")
@ApiSupport(author = "initcap", order = 1)
public class CompetitorNoumenonController extends BaseController {

    @Autowired
    private CompetitorNoumenonService competitorNoumenonService;

    @ModelAttribute
    public CompetitorNoumenon get(@RequestParam(required = false) String id) {
        CompetitorNoumenon entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = competitorNoumenonService.get(id);
        }
        if (entity == null) {
            entity = new CompetitorNoumenon();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @GetMapping("list")
    @ApiOperation("本体列表数据")
    public AjaxJson list(CompetitorNoumenon competitorNoumenon, HttpServletRequest request, HttpServletResponse response) {
        Page<CompetitorNoumenon> page = competitorNoumenonService.findPage(new Page<>(request, response), competitorNoumenon);
        return AjaxJson.success().put("page", page);
    }

    @RequiresPermissions("user")
    @GetMapping("queryById")
    @ApiOperation("根据Id获取本体数据")
    public AjaxJson queryById(CompetitorNoumenon competitorNoumenon) {
        return AjaxJson.success().put("competitorNoumenon", competitorNoumenon);
    }

    @RequiresPermissions(value = {"competitor:competitorNoumenon:add", "competitor:competitorNoumenon:edit"}, logical = Logical.OR)
    @PostMapping("save")
    @ApiOperation("保存本体")
    public AjaxJson save(CompetitorNoumenon competitorNoumenon) {
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(competitorNoumenon);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }
        //新增或编辑表单保存
        competitorNoumenonService.save(competitorNoumenon);
        return AjaxJson.success("保存本体成功");
    }

    @RequiresPermissions("competitor:competitorNoumenon:del")
    @DeleteMapping("delete")
    @ApiOperation("批量删除本体")
    public AjaxJson delete(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            competitorNoumenonService.delete(competitorNoumenonService.get(id));
        }
        return AjaxJson.success("删除本体成功");
    }

    @RequiresPermissions("competitor:competitorNoumenon:export")
    @GetMapping("export")
    @ApiOperation("导出本体")
    public void export(CompetitorNoumenon entity, HttpServletResponse response) {
        ExcelKit.$Export(CompetitorNoumenon.class, response).downXlsx(competitorNoumenonService.findList(entity), false);
    }

    @RequiresPermissions("competitor:competitorNoumenon:import")
    @GetMapping("template")
    @ApiOperation("导出本体模版")
    public void export(HttpServletResponse response) {
        ExcelKit.$Export(CompetitorNoumenon.class, response).downXlsx(new ArrayList<>(), true);
    }

    @RequiresPermissions("competitor:competitorNoumenon:import")
    @PostMapping(value = "import")
    @ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
    @ApiOperation("导入文件,下载模版为/export")
    public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
        if (shinProperites.isDemoMode()) {
            return AjaxJson.error("演示模式，不允许操作！");
        }
        ImportExcelResult<CompetitorNoumenon> result = competitorNoumenonService.uploadExcel(file, modle);
        return AjaxJson.success(result);
    }

    @RequiresPermissions("user")
    @GetMapping("getNoumenonTreeStructure")
    @ApiOperation("获取本体树结构")
    public AjaxJson list(CompetitorNoumenonDTO competitorNoumenonDTO) throws Exception {
        List<CompetitorNoumenonResultVO> competitorNoumenonResultVOList = competitorNoumenonService.getNoumenonTreeStructure(competitorNoumenonDTO);
        for (CompetitorNoumenonResultVO competitorNoumenonResultVO : competitorNoumenonResultVOList) {
            Map<String, CompetitorDataItemVO> map = competitorNoumenonResultVO.getCompetitorDataItemVOS().stream()
                    .collect(Collectors.toMap(CompetitorDataItemVO::getDataItemId, o -> o, (o, o1) -> o));
            competitorNoumenonResultVO.setCompetitorDataItemVOMap(map);
            competitorNoumenonResultVO.setCompetitorDataItemVOS(null);
        }
        return AjaxJson.success().put("data", competitorNoumenonResultVOList);
    }

    @RequiresPermissions("user")
    @GetMapping("conditionTree")
    @ApiOperation("条件树形结构")
    public AjaxJson conditionTree(String brandId) throws Exception {
        List<ConditionTreeVO> list = competitorNoumenonService.conditionTree(brandId);
        List<ConditionTreeVO> conditionTreeVOList = TreeUtilCommon.buildTree(list, "com.shinbada.modules.competitor.entity.VO.ConditionTreeVO", "id", "pid", "children");
        return AjaxJson.success().put("data", conditionTreeVOList);
    }

    @RequiresPermissions("user")
    @PostMapping("modelSelection")
    @ApiOperation("参数选型")
    public AjaxJson modelSelection(@RequestBody List<ParamModelSelectionDTO> paramModelSelectionDTOList) throws Exception {
        List<CompetitorNoumenonResultVO> competitorNoumenonResultVOList = competitorNoumenonService.modelSelection(paramModelSelectionDTOList);
        return AjaxJson.success().put("data", competitorNoumenonResultVOList);
    }
    @RequiresPermissions("user")
    @GetMapping("competitorRecommend")
    @ApiOperation("竞品推荐")
    public AjaxJson competitorRecommend(CompetitorNoumenonDTO competitorNoumenonDTO){
        List<CompetitorNoumenonResultVO> competitorNoumenonResultVOList = competitorNoumenonService.competitorRecommend(competitorNoumenonDTO);
        return AjaxJson.success().put("data", competitorNoumenonResultVOList);
    }
    @RequiresPermissions("user")
    @GetMapping("identicalEntry")
    @ApiOperation("相同项处理")
    public AjaxJson identicalEntry(CompetitorNoumenonDTO competitorNoumenonDTO){
        List<CompetitorProductDataItemVO> competitorProductDataItemVOS = competitorNoumenonService.identicalEntry(competitorNoumenonDTO);
        return AjaxJson.success().put("data", competitorProductDataItemVOS);
    }
}
